<?php

namespace Syrius\Adapter;

/**
 * Classe che permette di ottenere delle informazioni a runtime sulla connessione del database
 * @package Adapter
 * @author <domenico.biancardi@gmail.com>
 */
class Stat {


	private $_engine = array();

	private $_charset = array();

	private $_collation = array();

	
	/**
	 * Recupero della connessione
	 */
	public function __construct($name = "default") {
		$this->_driver = Connection::getConnection($name);
	}

	public function getCharsetName() {
	}

	/**
	 * Ritorna i valori client_info e client_version sotto forma di array
	 */
	public function getClientInfo() {
	}

	public function getConnectionStats() {
	}

	private $driver;

	public function options() {
	}

	public function getThreadSafe() {
	}

	/**
	 * Acquisisce le informazioni sul sistema come ENGINE, CHARSET, COLLATION
	 */
	public function autoLearn() {
		$cache = \Syrius\Config\Config::getAutoLearn();
		if (count($cache) > 0) {
			$this->_engine = $cache ["engine"];
			$this->_charset = $cache ["charset"];
			$this->_collation = $cache ["collation"];
			return;
		}

		$query = new \Syrius\Query\Show_Engine ();
		$sql = new Query ($query);
		$result = new Result ($sql);
		foreach ($result as $item) {
			$this->_engine [] = $item;
		}
		$query = new \Syrius\Query\Show_Charset ();
		$sql = new Query ($query);
		$result = new Result ($sql);
		foreach ($result as $item) {
			$this->_charset [] = $item;
		}
		$query = new \Syrius\Query\Show_Collation ();
		$sql = new Query ($query);
		$result = new Result ($sql);
		foreach ($result as $item) {
			$this->_collation [] = $item;
		}

		\Syrius\Config\Config::setAutoLearn(array("engine" => $this->_engine, "charset" => $this->_charset, "collation" => $this->_collation));
	}

	public function tableExists($table) {
		$query = new \Syrius\Query\Show_Table ();
		$query->setTable($table);
		$sql = new Query ($query);
		$result = new Result ($sql);
		if (!$result->count())
			throw new \Syrius\Exception\Table_Not_Exists($table);
	}

	/**
	 * Ritorna un'array di colonne per una tabellla specifica
	 */
	public function getColumnsForTable($table) {
		$query = new \Syrius\Query\Show_Columns ();
		$query->setTable($table);
		$ret = array();
		try {
			$sql = new Query ($query);
			$result = new Result ($sql);

			foreach ($result as $item) {
				$ret [] = $item;
			}
		} catch (Exception $e) {
			print_R($e);
		}
		return $ret;
	}

	/**
	 * Ritorna le colonne che hanno delle relazioni per la tabella <table>
	 */
	public function getRelationForTable($table) {
	}

	/**
	 *
	 */
	public function getKeysForTable($table = "") {
		$query = new \Syrius\Query\Show_Keys ();
		$query->setTable($table);
		$ret = array();
		try {
			$sql = new Query ($query);
			$result = new Result ($sql);

			foreach ($result as $item) {
				$ret [] = $item;
			}
		} catch (Exception $e) {
			print_R($e);
		}
		return $ret;
	}

}

?>
